<?php
$custom = get_post_custom($post->ID);
$realestate_price = isset($custom["price"][0]) ? $custom["price"][0] : '';
if ($realestate_price <> '') $realestate_price = get_option('realestate_cur_sign') . $realestate_price;

$realestate_school = isset($custom["realestate_school"][0]) ? $custom["realestate_school"][0] : '';
$realestate_bedrooms_number = isset($custom["realestate_bedrooms_number"][0]) ? $custom["realestate_bedrooms_number"][0] : '';
$realestate_bathrooms_number = isset($custom["realestate_bathrooms_number"][0]) ? $custom["realestate_bathrooms_number"][0] : '';
$realestate_garages_number = isset($custom["realestate_garages_number"][0]) ? $custom["realestate_garages_number"][0] : '';
$realestate_square_footage = isset($custom["realestate_square_footage"][0]) ? $custom["realestate_square_footage"][0] : '';

$realestate_band =  isset($custom["realestate_band"][0]) ? $custom["realestate_band"][0] : '';
$integrate_gmaps = ( isset($custom["integrate_gmaps"][0]) && $custom["integrate_gmaps"][0] == 1 ) ? true : false;
$realestate_address = isset($custom["realestate_address"][0]) ? $custom["realestate_address"][0] : '270 Park Ave. New York';

$realestate_title = isset($custom["realestate_title"][0]) ? $custom["realestate_title"][0] : wp_title();

$custom["thumbs"] = isset($custom["thumbs"][0]) ? unserialize($custom["thumbs"][0]) : array();
$featured_video = isset($custom["featured_video"][0]) ? $custom["featured_video"][0] : '';

if(isset($_POST['submitted']))
{
	if(trim($_POST['contactName']) === '') {
		$nameError = 'Please enter your name.';
		$hasError = true;
	}
	else
	{
		$name = trim($_POST['contactName']);
	}
	
    if(trim($_POST['subjectName']) === '') {
		$nameError = 'Please enter your subject.';
		$hasError = true;
	}
	else
	{
		$subject_name = trim($_POST['subjectName']);
	}
	if(trim($_POST['email']) === '')  {
		$emailError = 'Please enter your email address.';
		$hasError = true;
	}
	else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
		$emailError = 'You entered an invalid email address.';
		$hasError = true;
	}
	else
	{
		$email = trim($_POST['email']);
	}
	if(trim($_POST['comments']) === '') {
		$commentError = 'Please enter a message.';
		$hasError = true;
	}
	else
	{
		$comments = trim($_POST['comments']);
	}
	if(!isset($hasError))
	{
		$emailTo = get_option('realestate_contact_email');
		if (!isset($emailTo) || ($emailTo == ''))
		{
			$emailTo = get_option('admin_email');
		}
		$subject = $subject_name.' - '.$realestate_title.' - Contact Form';
		$body = "Name: $name \n\nEmail: $email \n\nComments: $comments";
		$headers = 'From: '.$name.' <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
		wp_mail($emailTo, $subject, $body, $headers);
		$emailSent = true;
		@wp_redirect(home_url(), 301);
	}
}
?>

<?php if ($realestate_band <> '') { ?>
   <span class="band<?php echo(' '.esc_attr($realestate_band)); ?>"></span>
<?php }; ?>

<?php if (!empty($custom["thumbs"])) { ?>
   <div id="product-slider">
      <div id="product-slides">
         <?php for ($i = 0; $i <= count($custom["thumbs"])-1; $i++) { ?>
            <div class="item-slide">
               <a href="<?php echo esc_url($custom["thumbs"][$i]); ?>" rel="gallery" class="fancybox" title="<?php the_title(); ?>">
				  <?php echo realestate_new_thumb_resize( realestate_multisite_thumbnail($custom["thumbs"][$i]), 293, 293 ); ?>
                  <span class="overlay"></span>
               </a>
            </div> <!-- .item-slide -->
         <?php }; ?>
      </div> <!-- #product-slides -->
      
      <?php if ($realestate_price <> '') { ?>
         <span class="price2"><span><?php echo esc_html($realestate_price); ?></span></span>
      <?php }; ?>
   </div> <!-- #product-slider -->
<?php }; ?>

<div class="product-info">
   <h1 class="title"><?php the_title(); ?></h1>
   
   <div class="product-types clearfix">
      <?php if (get_the_term_list( $post->ID, "realestate_properties_type", '', ', ', '' ) <> '') { ?>
         <span class="type-icon small"><?php echo get_the_term_list( $post->ID, "realestate_properties_type", '', ', ', '' ) ?></span>
      <?php } ?>
      <?php if ($realestate_bedrooms_number <> '') { ?>
         <span class="bed-icon"><?php echo esc_html($realestate_bedrooms_number); ?> <?php esc_html_e('Bedroom', 'RealEstate');  ?></span>
      <?php } ?>
      <?php if ($realestate_bathrooms_number <> '') { ?>
         <span class="bath-icon"><?php echo esc_html($realestate_bathrooms_number); ?> <?php esc_html_e('Bathroom', 'RealEstate');  ?></span>
      <?php } ?>
      <?php if ($realestate_garages_number <> '') { ?>
           <span class="garage-icon"><?php echo esc_html($realestate_garages_number); ?> <?php esc_html_e('Garage', 'RealEstate');  ?></span>
        <?php } ?>
      <?php if ($realestate_square_footage <> '') { ?>
         <span class="ruler-icon"><?php echo esc_html($realestate_square_footage); ?> <?php esc_html_e('ft2', 'RealEstate');  ?></span>
      <?php } ?>
   </div> <!-- .product-types -->
   <div class="product-types clearfix">
      <?php if ($realestate_school <> '') { ?>
         <span class="school-icon"><?php echo esc_html($realestate_school); ?> <?php esc_html_e('School', 'RealEstate'); ?></span>
      <?php } ?>
   </div> <!-- .product-types -->
   
   <div class="description">
      <p><?php the_content(); ?></p>
   </div> <!-- .description -->
   
	<?php if (get_the_term_list( $post->ID, "realestate_properties_features", '', ', ', '' ) <> '') { ?>
		<div class="realestate_feature_list">
			<h2><?php _e('Property Features',get_current_theme());?></h2>
			<ul class="clearfix">
				<?php get_features("type=realestate_properties_features&format=list&links=false"); ?>
			</ul>
		</div>
   <?php } ?>
	<?php if (get_the_term_list( $post->ID, "realestate_properties_communitys", '', ', ', '' ) <> '') { ?>
		<div class="community_feature_list">
			<h2><?php _e('Community Features',get_current_theme());?></h2>
			<ul class="clearfix">
				<?php get_features("type=realestate_properties_communitys&format=list&links=false"); ?>
			</ul>
		</div>
	<?php } ?> 
   
<!-- VIDEO START -->
<script type='text/javascript' src='<?php bloginfo('template_directory'); ?>/jwplayer.js'></script>

<div id='mediaplayer'></div>

<script type="text/javascript">
  jwplayer('mediaplayer').setup({
    'flashplayer': '<?php bloginfo('template_directory'); ?>/player.swf',
    'id': 'playerID',
    'width': '320',
    'height': '294',
    'controlbar': 'bottom',
    'file': '<?php echo $featured_video; ?>',
    'image': '<?php bloginfo('template_directory'); ?>/player.jpg',
    'plugins': {
       'captions-2': {
           'back': 'true'
       }
    }
  });
</script>
<!-- VIDEO END -->

</div> <!-- #product-info -->

<div class="clear"></div>

<?php if (count($custom["thumbs"]) > 1) { ?>
   <div id="product-thumbs" class="clearfix">
      <div id="product-thumb-items">
         <div id="smallthumbs">
            <?php for ($i = 0; $i <= count($custom["thumbs"])-1; $i++) { ?>
               <a href="#" class="small-controller<?php if($i==0) echo(' active'); if ($i==count($custom["thumbs"])-1) echo(' last') ?>" rel="<?php echo($i+1); ?>">
                  <?php echo realestate_new_thumb_resize( realestate_multisite_thumbnail($custom["thumbs"][$i]), 49, 49 ); ?>
                  <span class="overlay"></span>
               </a>
            <?php }; ?>
         </div>
         <a href="#" id="left-arrow"><?php esc_html_e('Previous', 'RealEstate');?></a>
         <a href="#" id="right-arrow"><?php esc_html_e('Next', 'RealEstate');?></a>
      </div> <!-- #product-thumb-items -->
   </div> <!-- #product-thumbs -->
<?php }; ?>

<div class="clear"></div>

<?php if ($integrate_gmaps) { ?>
   <div id="gmaps-border">
      <div id="gmaps-container"></div>
   </div> <!-- end #gmaps-border -->

<script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3.1&sensor=true"></script>
   <script type="text/javascript">
      //<![CDATA[
      var map;
      var geocoder;

      initialize();

      function initialize() {
         geocoder = new google.maps.Geocoder();
         geocoder.geocode({
            'address': '<?php echo esc_js($realestate_address); ?>',
            'partialmatch': true}, geocodeResult);   
      }

      function geocodeResult(results, status) {
         
         if (status == 'OK' && results.length > 0) {         
            var latlng = new google.maps.LatLng(results[0].geometry.location.b,results[0].geometry.location.c);
			var myOptions = {
			   zoom: 10,
			   center: results[0].geometry.location,
			   mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			
			map = new google.maps.Map(document.getElementById("gmaps-container"), myOptions);
			   var marker = new google.maps.Marker({
			   position: results[0].geometry.location,
			   map: map,
			   title:"<?php the_title(); ?>"
			});

            var contentString = '<div id="content">'+
            '<h3 id="firstHeading" class="firstHeading" style="padding-bottom: 15px;">'+marker.title+'</h3>'+
            '<div id="bodyContent">'+
            '<p><a target="_blank" href="http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q='+escape(results[0].formatted_address)+'&amp;ie=UTF8&amp;view=map">'+results[0].formatted_address+'</a>'+
            '</p>'+
            '</div>'+
            '</div>';

            var infowindow = new google.maps.InfoWindow({
               content: contentString,
               maxWidth: 300
            });

            google.maps.event.addListener(marker, 'click', function() {
               infowindow.open(map,marker);
            });

            google.maps.event.trigger(marker, "click");

         } else {
            //alert("Geocode was not successful for the following reason: " + status);
         }
      }
      //]]>
   </script>
<?php } ?>
	<!-- CONTACT START -->
<div class="product-info">
   <h1 class="title">Contact</h1>
   
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
	<label for="contactName"><?php esc_html_e('Name', 'RealEstate'); ?>:</label>
	<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="required requiredField" />
	<?php if($nameError != '') { ?>
		<span class="error"><? esc_html_e($nameError, 'RealEstate'); ?></span>
	<?php } ?>
	<label for="subjectName"><?php esc_html_e('Subject', 'RealEstate'); ?>:</label>
	<input type="text" name="subjectName" id="subjectName" value="<?php if(isset($_POST['subjectName'])) echo $_POST['subjectName'];?>" class="required requiredField" />
	<?php if($subjectError != '') { ?>
		<span class="error"><? esc_html_e($subjectError, 'RealEstate'); ?></span>
	<?php } ?>
	<label for="email"><?php esc_html_e('Email', 'RealEstate'); ?>:</label>
	<input type="text" name="email" id="email" value="<?php if(isset($_POST['email']))  echo $_POST['email'];?>" class="required requiredField email" />
	<?php if($emailError != '') { ?>
		<span class="error"><? esc_html_e($emailError, 'RealEstate'); ?></span>
	<?php } ?>
	<label for="commentsText"><?php esc_html_e('Message', 'RealEstate'); ?>:</label>
	<textarea name="comments" id="commentsText" rows="20" cols="30" class="required requiredField">
		<?php if(isset($_POST['comments'])) echo $_POST['comments']; ?>
	</textarea>
	<?php if($commentError != '') { ?>
		<span class="error"><?=$commentError;?></span>
	<?php } ?>
	<button type="submit"><?php esc_html_e('Send Message', 'RealEstate'); ?></button>
	<input type="hidden" name="submitted" id="submitted" value="true" />
</form>
</div>
	<!-- CONTACT END -->
<?php wp_link_pages(array('before' => '<p><strong>'.esc_html__('Pages', 'RealEstate').':</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
<?php edit_post_link(esc_html__('Edit this page', 'RealEstate')); ?>